package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.Editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Builder;
import com.example.covid19.entity.Room;
import com.example.covid19.entity.Student;
import com.example.covid19.entity.Studentinfo;
import com.example.covid19.service.BuilderService;
import com.example.covid19.service.MajeurService;
import com.example.covid19.service.RoomService;
import com.example.covid19.service.StudentinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Stream;

import static org.apache.commons.lang.StringUtils.isNotBlank;

/**
 * <p>
 * VIEW 前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-03-11
 */
@RestController
@RequestMapping("/studentinfo")
public class StudentinfoController {
    private final StudentinfoService studentinfoService;
    private final RoomService roomService;
    @Autowired
    MajeurService majeurService;
    @Autowired
    BuilderService builderService;

    public StudentinfoController(StudentinfoService studentinfoService, RoomService roomService) {
        this.studentinfoService = studentinfoService;
        this.roomService = roomService;
    }
    private String getFieldValue(Studentinfo student, String fieldName) {
        try {
            Field field = student.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return (String) field.get(student);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 管理界面挂载时候的学生信息
     * @return StudentInfoList
     */
    @RequestMapping("/glstudentinfo")
    public Result glstudentinfo(){
        return Result.success(200,"获取",studentinfoService.list());
    }
    /**
     *
     * @param student
     * @return后台界面查询 条件为空则不查
     */
    @RequestMapping("filtestudent")
    public Result filtestudent(@RequestBody Studentinfo student){
        QueryWrapper<Studentinfo> queryWrapper = new QueryWrapper<>();
        Stream.of("name", "phone", "studentid")
                .filter(fieldName -> isNotBlank(getFieldValue(student, fieldName)))
                .forEach(fieldName -> queryWrapper.eq(fieldName, getFieldValue(student, fieldName)));
        List<Studentinfo> studentList = studentinfoService.list(queryWrapper);
        return Result.success(studentList);
    }

    /**
     *
     * @return不想再写接口了直接拿来挂载到换寝页面
     */
    @RequestMapping("/getchaninfo")
    public Result getchaninfo(HttpServletRequest httpServletRequest){
        String s = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        return Result.success(200,"获取",studentinfoService.list(new QueryWrapper<Studentinfo>().eq("studentid",s)));
    }
    @RequestMapping("getStuinfo")
    public Result getStuinfo(@RequestBody Editdto editdto){
        return Result.success(200,"ok",studentinfoService.getOne(new QueryWrapper<Studentinfo>().eq("studentid",editdto.getUserid())));
    }
}
